package com.jgp.security.shiro.service;

import com.jgp.security.pojo.PowerInfo;
import com.jgp.security.pojo.UserInfo;
import com.jgp.security.secmodel.SecUser;
import com.jgp.security.service.SecPowerService;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-11-04
 */
@Service
public class ShiroPowerService {
    
    @Autowired
    private SecPowerService powerService;
    
    public UserInfo packageUserInfo(SecUser user,String endPointType){
        return powerService.packageUserInfo(user,endPointType);
    }
    
    public SimpleAuthorizationInfo authorizationInfo(UserInfo userInfo){
        
        PowerInfo powerInfo = powerService.queryPowerInfo(userInfo);
    
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        if (powerInfo.getResources().size() > 0) {
            info.addStringPermissions(powerInfo.getResources());
        }
    
        if(powerInfo.getRoles().size()>0){
            info.addRoles(powerInfo.getRoles());
        }
        return info;
    }
}
